package com.yoomiito.app.adapter;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.yoomiito.app.R;
import com.yoomiito.app.listener.ItemClickListener;
import com.yoomiito.app.model.HaoDanKuInfo;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.widget.CouponView;

import java.util.List;

public class CouponGoodsAdapter extends DelegateAdapter.Adapter<RecyclerView.ViewHolder> {

    private List<HaoDanKuInfo> data;
    private LayoutHelper mLayoutHelper;
    private Context mContext;
    private ItemClickListener mItemClickListener;

    public CouponGoodsAdapter(Context context, @Nullable List<HaoDanKuInfo> data, LayoutHelper layoutHelper) {
        this.data = data;
        this.mLayoutHelper = layoutHelper;
        this.mContext = context;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_goods,parent,false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        HaoDanKuInfo info = data.get(position);
        ItemViewHolder viewHolder = (ItemViewHolder) holder;
        ImageLoader.getInstance().into(mContext,info.getItempic(),viewHolder.mGoodsPicIv);
        viewHolder.mGoodsNameTv.setText(info.getItemtitle());
        if (SpannableUtils.isEmptyOr0(info.getCouponmoney())){
            viewHolder.mCouponTv.setHeaderTextVisible(false);
            viewHolder.mCouponTv.setText("特价精选");
        }else {
            viewHolder.mCouponTv.setHeaderTextVisible(true);
            viewHolder.mCouponTv.setText("¥"+info.getCouponmoney());
        }
        viewHolder.mPriceTv.setText("¥"+SpannableUtils.handlePrice(info.getItemendprice()));
        viewHolder.mStoreTv.setText(info.getShopname());
        viewHolder.mSizeTv.setText("月销"+ SpannableUtils.saleNum(info.getItemsale()));
        viewHolder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemClickListener!=null){
                    mItemClickListener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder{

        ImageView mGoodsPicIv;
        TextView mGoodsNameTv;
        CouponView mCouponTv;
        TextView mPriceTv;
        TextView mStoreTv;
        TextView mSizeTv;
        View mView;
        public ItemViewHolder(View itemView) {
            super(itemView);
            mGoodsPicIv = itemView.findViewById(R.id.item_goods_img);
            mGoodsNameTv = itemView.findViewById(R.id.goodsTitle);
            mCouponTv = itemView.findViewById(R.id.couponView);
            mPriceTv = itemView.findViewById(R.id.goodsPrice);
            mStoreTv = itemView.findViewById(R.id.item_store_name);
            mSizeTv = itemView.findViewById(R.id.item_sale_size);
            itemView.findViewById(R.id.awardView).setVisibility(View.GONE);
            mView = itemView.findViewById(R.id.item_goods_cardView);
        }
    }
}
